<?php
class MemberModel extends Db {
	protected $_member_node = 'w_node';
	protected $_member_list = 'w_member_list';
	protected $_member_group = 'w_member_group';
	protected $_action_log = 'w_action_log'; // 操作记录
	protected $_coupons_history = 'w_coupons_history'; // 操作记录
	protected $_money_history = 'w_money_history'; // 操作记录
	protected $_face_list = 'w_face_list';
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $params
	 */
	public function addFaceList($params) {
		$this->add ( $this->_face_list, $params );
	}
	public function getFaceList() {
		return $this->getAll ( $this->_face_list );
	
		//return $this->getPage($start, $num, $this->_face_list);
	}
	public function delFaceOne($where) {
		return $this->delete ( $this->_face_list, $where );
	}
	public function getOneFace($where) {
		return $this->getOne ( $this->_face_list, $where );
	}
	/**
	 * 统计最近三十天的登录人数
	 * Enter description here ...
	 */
	public function tjLogin($days, $format = "%Y%m%d") {
		$sql = "select count(id) num,FROM_UNIXTIME(date,'$format') m from w_log GROUP by m order by m desc limit $days";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 获取随机的用户
	 * Enter description here ...
	 * @param unknown_type $limit
	 */
	public function getRandFace($limit) {
		$sql = "SELECT real_name,face,area FROM $this->_member_list WHERE level=1 ORDER BY rand() LIMIT $limit";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 修改密码
	 *
	 * @param array $v        	
	 * @param array $where        	
	 */
	public function setMemberByWhere($v, $where) {
		$this->update ( $this->_member_list, $v, $where );
	}
	/**
	 * 统计用户总账余额
	 * 
	 */
	public function getSumCouponsMember() {
		$sql = "SELECT sum(coupons) total FROM $this->_member_list";
		return $this->fetch ( $sql );
	}
	public function getCouponsMember() {
		$sql = "SELECT uid,coupons FROM w_member_list";
		return $this->fetchAll ( $sql );
	}
	public function getMemberByWhere($where, $key, $order_by, $limit) {
		return $this->getAll ( $this->_member_list, $where, $key, $order_by, $limit );
	}
	/**
	 * 增加积分
	 * Enter description here ...
	 * @param unknown_type $coupons
	 * @param unknown_type $uid
	 */
	public function updateMemberCoupons($coupons, $uid) {
		$sql = "UPDATE $this->_member_list SET coupons=coupons+$coupons WHERE uid=$uid";
		$this->exec ( $sql );
	}
	/**
	 * 删除用户
	 * Enter description here .
	 *
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $where        	
	 */
	public function delMemberByWhere($where) {
		$this->delete ( $this->_member_list, $where );
	}
	/**
	 *
	 *
	 *
	 *
	 *
	 * 添加操作记录
	 *
	 * @param array $params        	
	 */
	public function addActionLog($params) {
		$this->add ( $this->_action_log, $params );
	}
	/**
	 * 添加用户组
	 *
	 * @param array $arr        	
	 */
	public function addMemberGroup($arr) {
		$this->add ( $this->_member_group, $arr );
	}
	/**
	 * 删除用户组
	 *
	 * @param array $arr        	
	 */
	public function delMemberGroup($arr) {
		$this->delete ( $this->_member_group, $arr );
	}
	/**
	 * 更新用户组
	 *
	 * @param array $v        	
	 * @param array $where        	
	 */
	public function updateMemberGroup($v, $where) {
		$this->update ( $this->_member_group, $v, $where );
	}
	/**
	 * 获取一个用户组权限
	 *
	 * @param string $controller        	
	 * @param string $action        	
	 */
	public function getGroupNode($controller = "", $action = "") {
		$sql = "SELECT `value`,`node` FROM $this->_member_node WHERE `node`=\"$controller\" OR `node`=\"$action\"";
		$rs = $this->fetchAll ( $sql );
		return $rs;
	}
	/**
	 * 获取一个用户组信息
	 *
	 * @param array $where        	
	 */
	public function getOneGroup($where) {
		$rs = $this->getOne ( $this->_member_group, $where );
		return $rs;
	}
	/**
	 * 获取所欲用户组
	 */
	public function getGroupAll() {
		$rs = $this->getAll ( $this->_member_group );
		return $rs;
	}
	/**
	 * 获取一个用户信息
	 *
	 * @param
	 * $where
	 */
	public function getOneMember($where) {
		$rs = $this->getOne ( $this->_member_list, $where );
		return $rs;
	}
	/**
	 *
	 *
	 *
	 *
	 *
	 * 添加用户
	 *
	 * @param array $arr        	
	 */
	public function addMember($arr) {
		$this->add ( $this->_member_list, $arr );
		$id = $this->lastInsertId ();
		return $id;
	}
	/**
	 *
	 *
	 *
	 *
	 *
	 * 用户更新
	 *
	 * @param array $v        	
	 * @param array $where        	
	 */
	public function updateMember($v, $where) {
		$this->update ( $this->_member_list, $v, $where );
	}
	/**
	 * 获取所有用户
	 * Enter description here .
	 *
	 *
	 *
	 * ..
	 */
	public function getMemberAll() {
		$rs = $this->getAll ( $this->_member_list );
		return $rs;
	}
	/**
	 * 获取用户名和所属分组
	 */
	public function getAllMemberInfo($group) {
		$sql = "SELECT a.`username`,b.`name` FROM w_member_list a LEFT JOIN w_member_group b ON a.group=b.id WHERE a.group=$group";
		return $result = $this->fetchAll ( $sql );
	}
	/**
	 * 获取用户信息 包含用户权限
	 * Enter description here .
	 *
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $uid        	
	 */
	public function getMemberAndGroup($uid) {
		$sql = "SELECT * FROM w_member_list a LEFT JOIN w_member_group b ON a.group=b.id WHERE a.id=$uid";
		return $result = $this->fetch ( $sql );
	
		// Benchmark::print_r($result);
	}
	public function getPages($start, $num, $where, $order) {
		if (empty ( $where )) {
			$sql = "SELECT * FROM w_member_list a LEFT JOIN w_member_group b ON a.groupid=b.id   ORDER BY a.uid DESC LIMIT $start,$num ";
		} else {
			$where = $this->batchWhere ( $where );
			$sql = "SELECT * FROM w_member_list a LEFT JOIN w_member_group b ON a.groupid=b.id WHERE $where  ORDER BY a.uid DESC LIMIT $start,$num ";
		}
		return $this->fetchAll ( $sql );
	
		//   return $this->getPage($start, $num, $this->_member_list, null, $where, $order);
	}
	/**
	 * 模糊搜索
	 * @param unknown $key
	 * @param unknown $v
	 * @return multitype:
	 */
	public function getMemberByLike($key, $v) {
		$sql = "SELECT * FROM $this->_member_list a LEFT JOIN $this->_member_group b ON a.groupid=b.id WHERE $key LIKE '%$v%'";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 
	 * 更新积分值
	 * @param unknown_type $uid
	 * @param unknown_type $coupons  积分
	 * @param unknown_type $remark 渠道
	 * @param unknown_type $paymment 0收入 1支出
	 * @param unknown_type $chargetypes  充值 消费 奖励
	 */
	public function saveCoupons($uid, $coupons, $remark, $payment, $chargetypes, $orderno, $status) {
		$sql = "UPDATE $this->_member_list SET coupons=coupons+$coupons WHERE uid=$uid";
		$row = $this->exec ( $sql );
		//增加历史记录
		if ($row > 0) {
			return $this->add ( $this->_coupons_history, array ('uid' => $uid, 'coupons' => $coupons, 'date' => time (), 'payment' => $payment, 'chargetypes' => $chargetypes, 'remark' => $remark, 'orderno' => $orderno, 'status' => $status ) );
		}
		return 0;
	}
	/**
	 * 设置jife
	 * */
	public function setCouponsStatus($v, $where) {
		$this->update ( $this->_coupons_history, $v, $where );
	}
	//更新积分值
	public function saveMoney($uid, $money, $remark, $paymment, $chargetypes, $orderno, $status) {
		$sql = "UPDATE $this->_member_list SET money=money+$money WHERE uid=$uid";
		$row = $this->exec ( $sql );
		//增加历史记录
		if ($row > 0) {
			return $this->add ( $this->_money_history, array ('uid' => $uid, 'coupons' => $money, 'date' => time (), 'payment' => $paymment, 'chargetypes' => $chargetypes, 'remark' => $remark, 'orderno' => $orderno, 'status' => $status ) );
		}
		return 0;
	}
	/**
	 * 
	 * 指定时间收入
	 * */
	public function getIncomeByTime($starttime, $endtime, $where) {
		$where = $this->batchWhere ( $where );
		$sql = "select sum(coupons) income from w_coupons_history a WHERE payment=0 AND   `date`>=$starttime and `date`<=$endtime AND $where";
		return $this->fetch ( $sql );
	}
	/**
	 * 指定时间支出
	 * Enter description here ...
	 * @param unknown_type $endtime
	 * @param unknown_type $where
	 */
	public function getSpendByTime($starttime, $endtime, $where) {
		$where = $this->batchWhere ( $where );
		$sql = "select sum(coupons) spend from w_coupons_history a WHERE payment=1 AND  `date`>=$starttime and `date`<=$endtime AND $where";
		return $this->fetch ( $sql );
	}
	/**
	 * 期初收入
	 * Enter description here ...
	 * @param unknown_type $endtime
	 * @param unknown_type $where
	 */
	public function getIncomeByBeforeTime($endtime, $where) {
		$where = $this->batchWhere ( $where );
		$sql = "select sum(coupons) income from w_coupons_history a WHERE  payment=0 AND `date`<=$endtime AND $where";
		return $this->fetch ( $sql );
	}
	/**
	 * 期初支出
	 * Enter description here ...
	 * @param unknown_type $endtime
	 * @param unknown_type $where
	 */
	public function getSpendByBeforeTime($endtime, $where) {
		$where = $this->batchWhere ( $where );
		$sql = "select sum(coupons) spend from w_coupons_history a WHERE payment=1 AND   `date`<=$endtime AND $where";
		return $this->fetch ( $sql );
	}
	public function countMember($where = NULL) {
		if ($where != null) {
			$where = $this->batchWhere ( $where );
			$sql = "SELECT uid FROM $this->_member_list WHERE $where";
		} else {
			$sql = "SELECT uid FROM $this->_member_list";
		}
		return $this->rowCount ( $sql );
	}
	/**
	 * 获取充值记录
	 * Enter description here ...
	 * @param unknown_type $where
	 */
	public function getCouponsHistory($where = NULL) {
		return $this->getAll ( $this->_coupons_history, $where );
	}
	public function getCouponsHistoryPage($start, $num, $where = NULL) {
		if ($where != null) {
			$where = $this->batchWhere ( $where );
			$sql = "SELECT a.*,b.real_name,b.country,b.area FROM $this->_coupons_history a LEFT JOIN $this->_member_list b ON a.uid=b.uid  WHERE $where ORDER BY a.id DESC LIMIT $start,$num";
		} else {
			$sql = "SELECT a.*,b.real_name,b.country,b.area FROM $this->_coupons_history a LEFT JOIN $this->_member_list b ON a.uid=b.uid ORDER BY a.id DESC LIMIT $start,$num";
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 * 获取指定时间的订单
	 * @param unknown_type $starttime
	 * @param unknown_type $endtime
	 * @param unknown_type $where
	 */
	public function getCouponsHistoryAll($starttime, $endtime, $where = NULL) {
		if ($where != NULL) {
			$where = $this->batchWhere ( $where );
			$sql = "SELECT a.*,b.real_name,b.country,b.area FROM $this->_coupons_history a LEFT JOIN $this->_member_list b ON a.uid=b.uid WHERE a.date>=$starttime AND a.date<=$endtime AND $where";
		} else {
			$sql = "SELECT a.*,b.real_name,b.country,b.area FROM $this->_coupons_history a LEFT JOIN $this->_member_list b ON a.uid=b.uid WHERE a.date>=$starttime AND a.date<=$endtime ";
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 *
	 * 获得最新的会员信息
	 *
	 * @param int $num
	 * 获取条数
	 * @return array -id 用户唯一id
	 * username 用户名称
	 * face 用户头像
	 */
	public function getLastMember($num) {
		$sql = "SELECT `id`,`username`,`face` FROM $this->_member_list ORDER BY id DESC LIMIT $num";
		$rs = $this->fetchAll ( $sql );
		return $rs;
	}
	/**
	 * 返回MemberModel
	 *
	 * @return MemberModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}
}